﻿using System;
using System.Drawing;
using System.Web;
using System.Web.UI;
using JQuery.WebKit.Base;
using JQuery.WebKit.Util;

namespace JQuery.WebKit
{
    public class JImage : JControl
    {
        internal override PostType PostType { get { return PostType.Value; } }
        public new int Width { get; set; }

        public string Src { get; set; }
        public string Alt { get; set; }
        public int Height { get; set; }
        public string ImageIdentifier { get; set; }
        public new Image Value
        {
            get
            {
                string sessionKey = string.Format("Image_{0}", ImageIdentifier);
                return JUtil.Imaging.FromByteArray(HttpContext.Current.Session[sessionKey]);
            }
            set
            {
                string sessionKey = string.Format("Image_{0}", ImageIdentifier);
                HttpContext.Current.Session[sessionKey] = Resize(JUtil.Imaging.FromByteArray(value));
            }
        }

        public JImage() : base("img") { }
        public JImage(string tag) : base("img") { }

        protected override void CreateChildControls() { }

        protected override void Render(HtmlTextWriter writer)
        {
            if (Width != 0)
            {
                this.Attributes.Add("width", Width.ToString());
            }

            if (Height != 0)
            {
                this.Attributes.Add("height", Height.ToString());
            }

            this.Attributes.Add("src", Src);
            this.Attributes.Add("alt", Alt);

            base.Render(writer);
        }

        public Image Resize(Image image)
        {
            return JImage.Resize(image, Width, Height);
        }

        public static Image Resize(Image image, int width, int height)
        {
            if (image == null || (width == 0 && height == 0))
            {
                return image;
            }
            int w = width;
            int h = height;
            if (width != 0)
            {
                double aspect = (double)image.Width / (double)image.Height;
                if (aspect <= 1)
                {
                    h = Convert.ToInt32(Math.Floor((double)width / aspect));
                }
                else
                {
                    h = Convert.ToInt32(Math.Floor((double)width / aspect));
                }
            }
            else if (height != 0)
            {
                double aspect = (double)image.Width / (double)image.Height;
                if (aspect <= 1)
                {
                    w = Convert.ToInt32(Math.Floor((double)height * aspect));
                }
                else
                {
                    w = Convert.ToInt32(Math.Floor((double)height / aspect));
                }
            }
            return image.GetThumbnailImage(w, h, null, IntPtr.Zero);
        }

        protected override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.Write("<div style='position:relative'>");
        }
    }
}
